/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package components;

import dao.VeterinarianDAO;
import entity.Veterinarian;
import hpvas.HPVAS;
import java.text.ParseException;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.EntityExistsException;
import javax.persistence.Persistence;
import javax.swing.JFormattedTextField;
import javax.swing.JOptionPane;
import javax.swing.JSpinner;
import javax.swing.SpinnerDateModel;
import javax.swing.text.MaskFormatter;

/**
 *
 * @author Juan
 */
public class PVetRegistration extends javax.swing.JPanel {

    /**
     * Creates new form POwnerRegistration
     */
    public PVetRegistration() {
        initComponents();
        
        FTFSSN.setFormatterFactory(new JFormattedTextField.AbstractFormatterFactory() {

            @Override
            public JFormattedTextField.AbstractFormatter getFormatter(JFormattedTextField tf) {
                MaskFormatter mf = null;
                try {
                    mf = new MaskFormatter("###-##-####");
                } catch (ParseException ex) {
                    Logger.getLogger(PVetRegistration.class.getName()).log(Level.SEVERE, null, ex);
                } finally {
                    return mf;
                }
            }
        });
        
        SBirthday.setEditor(new JSpinner.DateEditor(SBirthday, "dd/MM/yyyy"));
        SBirthday.setValue(new Date());
        
        ComponentFactory.attachVerifier("Please provide the name of the owner.", TFName);
        ComponentFactory.attachVerifier("Please provide the last name of the owner.", TFLastname);
        ComponentFactory.attachVerifier("Please provide the birthplace of the owner.", TFBirthplace);
        ComponentFactory.attachVerifier("Please provide the focus of practice.", TFFocusOfPractice);
        ComponentFactory.attachVerifier("Please supply at least one speciality", TFSpecialities);
        ComponentFactory.attachVerifier("Please supply the graduation intitute.", TFGraduatedAt);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        FTFSSN = new javax.swing.JFormattedTextField();
        TFName = new javax.swing.JTextField();
        TFLastname = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        SBirthday = new javax.swing.JSpinner();
        jLabel6 = new javax.swing.JLabel();
        TFBirthplace = new javax.swing.JTextField();
        BRegisterVet = new javax.swing.JButton();
        BCancel = new javax.swing.JButton();
        jLabel7 = new javax.swing.JLabel();
        TFFocusOfPractice = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        TFSpecialities = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        FTFSalary = new javax.swing.JFormattedTextField();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        TFGraduatedAt = new javax.swing.JTextField();

        jLabel1.setText("Register Veterinarian:");

        jLabel2.setText("Name:");

        jLabel3.setText("Lastname:");

        jLabel4.setText("SSN:");

        FTFSSN.setText("XXX-XX-XXXX");

        jLabel5.setText("Birthday:");

        SBirthday.setModel(new javax.swing.SpinnerDateModel(new java.util.Date(), null, new java.util.Date(), java.util.Calendar.DAY_OF_MONTH));

        jLabel6.setText("Birthplace:");

        BRegisterVet.setText("Register Veterinarian");
        BRegisterVet.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BRegisterVetActionPerformed(evt);
            }
        });

        BCancel.setText("Cancel");
        BCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BCancelActionPerformed(evt);
            }
        });

        jLabel7.setText("Focus of Practice:");

        jLabel8.setText("Specialities (Space Separated):");

        jLabel9.setText("Salary:");

        FTFSalary.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0"))));

        jLabel10.setText("US$");

        jLabel11.setText("Graduated At:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(TFName)
                    .addComponent(FTFSSN)
                    .addComponent(TFLastname)
                    .addComponent(TFBirthplace)
                    .addComponent(TFFocusOfPractice)
                    .addComponent(TFSpecialities)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5)
                            .addComponent(SBirthday, javax.swing.GroupLayout.PREFERRED_SIZE, 192, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel6)
                            .addComponent(jLabel7)
                            .addComponent(jLabel8)
                            .addComponent(jLabel9)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(BRegisterVet)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(BCancel))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel10)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(FTFSalary, javax.swing.GroupLayout.PREFERRED_SIZE, 147, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jLabel11))
                        .addGap(0, 172, Short.MAX_VALUE))
                    .addComponent(TFGraduatedAt))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(TFName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(TFLastname, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(FTFSSN, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(SBirthday, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(TFBirthplace, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel7)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(TFFocusOfPractice, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel8)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(TFSpecialities, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel9)
                .addGap(4, 4, 4)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(FTFSalary, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel10))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel11)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(TFGraduatedAt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(BRegisterVet)
                    .addComponent(BCancel))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void BCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BCancelActionPerformed
        HPVAS.closeCurrentTab();
    }//GEN-LAST:event_BCancelActionPerformed

    private void BRegisterVetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BRegisterVetActionPerformed
        Veterinarian newVeterianrian = new Veterinarian();
        CustomVerifier currentVerifier;
        
        currentVerifier = (CustomVerifier) TFName.getInputVerifier();
        if (currentVerifier.isValid()) {
            newVeterianrian.setName(TFName.getText().trim());
        } else {
            return;
        }
        
        currentVerifier = (CustomVerifier) TFLastname.getInputVerifier();
        if (currentVerifier.isValid()) {
            newVeterianrian.setLastname(TFLastname.getText().trim());
        } else {
            return;
        }
        
        if (FTFSSN.isEditValid()) {
            newVeterianrian.setSSN(FTFSSN.getText());
        } else {
            JOptionPane.showMessageDialog(this.getTopLevelAncestor(), "The social security number provided is not valid.", "Warning", JOptionPane.WARNING_MESSAGE);
            FTFSSN.requestFocusInWindow();
            return;
        }
        
        SpinnerDateModel sdm = (SpinnerDateModel) SBirthday.getModel();
        newVeterianrian.setBirthday(new java.sql.Date(sdm.getDate().getTime()));
        
        currentVerifier = (CustomVerifier) TFBirthplace.getInputVerifier();
        if (currentVerifier.isValid()) {
            newVeterianrian.setBirthplace(TFBirthplace.getText());
        } else {
            return;
        }
        
        currentVerifier = (CustomVerifier) TFFocusOfPractice.getInputVerifier();
        if (currentVerifier.isValid()) {
            newVeterianrian.setFocusOfPractice(TFFocusOfPractice.getText().trim());
        } else {
            return;
        }
        
        currentVerifier = (CustomVerifier) TFSpecialities.getInputVerifier();
        if (currentVerifier.isValid()) {
            newVeterianrian.setSpecialities(TFSpecialities.getText().trim());
        } else {
            return;
        }
        
        if (FTFSalary.isEditValid()) {
            newVeterianrian.setSalary(Integer.parseInt(FTFSalary.getText()));
        } else {
            return;
        }
        
        currentVerifier = (CustomVerifier) TFGraduatedAt.getInputVerifier();
        if (currentVerifier.isValid()) {
            newVeterianrian.setGraduatedAt(TFGraduatedAt.getText());
        } else {
            return;
        }
        
        try {
            VeterinarianDAO vetDAO = new VeterinarianDAO(Persistence.createEntityManagerFactory("HPVA"));
            vetDAO.create(newVeterianrian);
            
            JOptionPane.showMessageDialog(this.getTopLevelAncestor(),
                    "The veterinarian has been succesfully registered.", "Info", JOptionPane.INFORMATION_MESSAGE);
            
            HPVAS.updateVeterinarians();
            HPVAS.closeCurrentTab();
        } catch (EntityExistsException entityExistsException) {
            JOptionPane.showMessageDialog(this.getTopLevelAncestor(),
                    "This veterinarian already exists in the database.", "Error", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_BRegisterVetActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BCancel;
    private javax.swing.JButton BRegisterVet;
    private javax.swing.JFormattedTextField FTFSSN;
    private javax.swing.JFormattedTextField FTFSalary;
    private javax.swing.JSpinner SBirthday;
    private javax.swing.JTextField TFBirthplace;
    private javax.swing.JTextField TFFocusOfPractice;
    private javax.swing.JTextField TFGraduatedAt;
    private javax.swing.JTextField TFLastname;
    private javax.swing.JTextField TFName;
    private javax.swing.JTextField TFSpecialities;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    // End of variables declaration//GEN-END:variables
}
